<!--
  - Copyright (c) 2021.  北京维数科技有限公司
  - Date :  2021/3/25
  - Author ：F12 Console=> atob("MzMxNDkxODAwQHFxLmNvbQ==")
  - Version ：1.0.0
  -->

<template>
  <div><div class="abstatic-height"></div>
  <div class="app-container calendar-list-container">
    <el-row>
      <div class="xf-tabletop-handles">
        <div class="xf-tabhd-left">
<!--          单选按钮  change事件实现-->
          <el-radio-group v-model="radioType" @change="switchTable" class="xf-tab-bts" size="mini">
            <el-radio-button label="sensor">传感器厂商</el-radio-button>
            <el-radio-button label="device">采集设备厂商</el-radio-button>
          </el-radio-group>
<!--          添加    根据if else 判断哪个添加按钮     -->
          <el-button class="filter-item" @click="handleCreate" type="primary" v-if="radioType==='sensor'" size="mini">添加厂商</el-button>
          <el-button class="filter-item" @click="handleCreateSimple" type="primary" v-else size="mini">添加采集设备厂商</el-button>
        </div>
<!--        查询条件       -->
        <div class="xf-tabhd-right">
          厂商
          <el-input v-model="listQuery.sensorManufacturer" placeholder="请输入厂商" v-if="radioType==='sensor'" class="xf-query-input" size="mini"></el-input>
          <el-input v-model="listQuery.collectingManufacturer" placeholder="请输入厂商" class="xf-query-input" v-else size="mini"></el-input>
          <el-button @click="tableQuery" type="primary" size="mini">查询</el-button>
        </div>
      </div>
    </el-row>
<!--    传感器厂商 表格  -->
    <el-table key='0' :data="list" v-loading.body="listLoading" style="width: 100%" v-if="radioType==='sensor'">
      <el-table-column label="序号" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column label="厂商">
        <template slot-scope="scope">
          <span>{{scope.row.sensorManufacturer}}</span>
        </template>
      </el-table-column>
<!--      传感器厂商 操作   -->
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <span @click="handleUpdate(scope.row)" class="comm-table-btn"><icon-svg icon-class="bianji" style="fill:#50E3C2" /></span>
          <span @click="handleDelete(scope.row)" class="comm-table-btn"><icon-svg icon-class="shanchu" style="fill:#50E3C2" /></span>
        </template>
      </el-table-column>
    </el-table>

<!--    采集设备厂商  表格-->
    <el-table key='1' :data="devicelist" v-loading.body="listLoading" style="width: 100%" v-else>
      <el-table-column label="序号" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column label="厂商">
        <template slot-scope="scope">
          <span>{{scope.row.collectingManufacturer}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <!--    采集设备厂商  修改删除操作-->
        <template slot-scope="scope">
          <span @click="deviceHandleUpdate(scope.row)" class="comm-table-btn"><icon-svg icon-class="bianji" style="fill:#50E3C2" /></span>
          <span @click="handleDelete(scope.row)" class="comm-table-btn"><icon-svg icon-class="shanchu" style="fill:#50E3C2" /></span>
        </template>
      </el-table-column>
    </el-table>

<!--    分页  -->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page"
                     :page-sizes="[15,20,30, 50]"
                     :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>


    <slider-panel :Show.sync="dialogFormVisible" custom-class="out-side-frame">
      <div class="form-content-frame">
        <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position='top' @submit.native.prevent>
          <el-form-item label="厂商" prop="sensorManufacturer">
            <el-input v-model.trim="form.sensorManufacturer" placeholder="请输入传感器厂商"></el-input>
          </el-form-item>
            <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')" size="mini" native-type="submit">确 定</el-button>
            <el-button v-else type="primary" @click="update('form')" size="mini" native-type="submit">确 定</el-button>
            <el-button @click="cancel('form')" size="mini" type="text">取 消</el-button>
        </el-form>
      </div>
    </slider-panel>
    <slider-panel :Show.sync="devicedialogFormVisible" custom-class="out-side-frame">
      <div class="form-content-frame">
        <el-form :model="deviceform" :rules="devicerules" ref="deviceform" label-width="100px" label-position='top' @submit.native.prevent>
          <el-form-item label="厂商" prop="collectingManufacturer">
            <el-input v-model.trim="deviceform.collectingManufacturer" placeholder="请输入采集设备厂商"></el-input>
          </el-form-item>
            <el-button v-if="dialogStatus=='create'" type="primary" @click="deviceCreate('deviceform')" size="mini" native-type="submit">确 定</el-button>
            <el-button v-else type="primary" @click="deviceUpdate('deviceform')" size="mini" native-type="submit">确 定</el-button>
            <el-button @click="deviceCancel('deviceform')" type="text" size="mini">取 消</el-button>
        </el-form>
      </div>
    </slider-panel>

    <!-- 删除操作弹窗 -->
    <el-dialog :title="radioType==='sensor'?'删除传感器厂商':'删除采集设备厂商'" :visible.sync="dialogDeleteVisible" :show-close='false' custom-class="xf-jc-dialog" width="360px">
      <div class="xf-jc-deletect" v-if="deleteJumpObj.length>0">
        <p>请将厂商与设备类型解绑后再进行删除</p>
        <div>
          <el-button type="text" @click="openChildsites">
            前往解绑
            <i class="el-icon-d-arrow-right"></i>
          </el-button>
        </div>
      </div>
      <div class="xf-jc-deletect" v-else>
        <p v-if="radioType==='sensor'">无子站使用此厂商，确定删除？</p>
        <p v-else>
          无子站使用此厂商，确定删除？
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" size="mini" v-if="deleteJumpObj.length<1" @click="deleteD(currentSelectObj)">删除</el-button>
        <el-button type="text" size="mini" v-if="deleteJumpObj.length<1" @click="dialogDeleteVisible = false">取消</el-button>
        <el-button type="primary" size="mini" v-if="deleteJumpObj.length>0" @click="dialogDeleteVisible = false">知道了</el-button>
      </div>
    </el-dialog>
    <childSiteDialog :Show.sync="childSitesShow" :siteIds.sync="childSiteids"></childSiteDialog>
  </div>
  </div>
</template>

<script>
import childSiteDialog from '@/components/ChildsiteList';

import {
  page,
  deleteQuery,
  deviceAddObj,
  deviceGetObj,
  devicePutObj,
  deviceDelObj,
  deviceDeleteQuery,
  devicePage,
  devicePoint,
  addObj,
  getObj,
  delObj,
  putObj
} from '@/api/admin/firm/index';
import { mapGetters } from 'vuex';
import sliderPanel from '@/components/SliderPanel'
export default {
  name: 'deviceSensorSeries',
  components: { childSiteDialog, sliderPanel },
  data() {
    return {
      childSitesShow: false,
      childSiteids: '',
      radioType: 'sensor',
      sensorId: '',
      deviceform: {
        collectingManufacturer: undefined
      },
      devicerules: {
        collectingManufacturer: [
          {
            required: true,
            message: '请输入采集设备厂商',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 100,
            message: '最多允许输入100个字符',
            trigger: 'blur'
          }
        ]
      },
      form: {
        sensorManufacturer: undefined
      },
      rules: {
        sensorManufacturer: [
          {
            required: true,
            message: '请输入传感器厂商',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 100,
            message: '最多允许输入100个字符',
            trigger: 'blur'
          }
        ]
      },
      list: null,
      devicelist: null,
      devicePoinsts: [],
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 15,
        sensorManufacturer: undefined,
        collectingManufacturer: undefined
      },
      dialogFormVisible: false,
      devicedialogFormVisible: false,
      dialogDeleteVisible: false,
      deleteJumpObj: '',
      currentSelectObj: {},
      dialogStatus: '',

      textMap: {
        update: '编辑传感器厂商',
        create: '添加传感器厂商'
      },
      gatetextMap: {
        update: '编辑采集设备厂商',
        create: '添加采集设备厂商'
      },
      tableKey: 0
    };
  },
  //初始化
  created() {
    this.getList();
  },
  computed: {},
  methods: {
    openChildsites() {
      this.$router.push('/adminSys/deviceSeries')
    },
    tableQuery() {
      if (this.radioType == 'sensor') {
        this.getList();
      } else {
        this.getDeviceList();
      }
    },
    //单选按钮实现
    switchTable() {
      // 切换按钮时重置分页查询条件
      this.listQuery.typeName = undefined;
      this.listQuery.type = undefined;
      if (this.radioType == 'sensor') {
        this.listQuery.page = 1;
        this.getList();
      } else {
        this.listQuery.page = 1;
        this.getDeviceList();
      }
    },
    //传感器厂商列表
    getList() {
      this.listLoading = true;
      page(this.listQuery).then(response => {
        this.list = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handlerest() {
      this.sensorId = '';
      this.form.sensorManufacturer = undefined;
    },
    deviceHandlerest() {
      this.deviceform.collectingManufacturer = undefined;
    },
    //采集设备厂商列表
    getDeviceList() {
      this.listLoading = true;
      devicePage(this.listQuery).then(response => {
        this.devicelist = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      }).catch(function(error) {
        this.listLoading = false;
        console.log(error);
      });
    },
    //分页size
    handleSizeChange(val) {
      this.listQuery.limit = val;
      if (this.radioType === 'sensor') {
        this.getList();
      } else {
        this.getDeviceList()
      }
    },
    //分页current
    handleCurrentChange(val) {
      this.listQuery.page = val;
      if (this.radioType === 'sensor') {
        this.getList();
      } else {
        this.getDeviceList()
      }
    },
    //添加厂商
    handleCreate() {
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
      this.form = {};
    },
    //添加采集厂商
    handleCreateSimple() {
      this.dialogStatus = 'create';
      this.devicedialogFormVisible = true;
      this.deviceform = {};
    },
    //修改传感器厂商
    handleUpdate(row) {
      getObj(row.id).then(response => {
        if (response.status === 200) {
          this.sensorId = response.data.id;
          this.form.sensorManufacturer = response.data.sensorManufacturer;
          this.dialogFormVisible = true;
          this.dialogStatus = 'update'
        }
      }).catch(error => {
        console.log(error);
      });
    },
    //修改采集设备厂商
    deviceHandleUpdate(row) {
      deviceGetObj(row.id).then(response => {
        this.deviceform = response.data;
        this.devicedialogFormVisible = true;
        this.dialogStatus = 'update';
      });
    },
    deleteD(row) {
      if (this.radioType === 'sensor') {
        delObj(row.id).then(() => {
          this.dialogDeleteVisible = false;
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getList();
        }).catch(error => {
          console.log(error);
        });
      } else {
        deviceDelObj(row.id).then(() => {
          this.dialogDeleteVisible = false;
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getDeviceList();
        }).catch(error => {
          console.log(error);
        });
      }
    },
    handleDelete(row) {
      // 删除操作查询
      const self = this;
      if (this.radioType === 'sensor') {
        deleteQuery(row.id).then(response => {
          if (response.status === 200) {
            self.currentSelectObj = row;
            self.dialogDeleteVisible = true;
            self.deleteJumpObj = response.data;
            self.childSiteids = response.data.join(',');
          }
        }).catch(error => {
          console.log(error);
        });
      } else {
        deviceDeleteQuery(row.id).then(response => {
          if (response.status === 200) {
            self.currentSelectObj = row;
            self.dialogDeleteVisible = true;
            self.deleteJumpObj = response.data;
            self.childSiteids = response.data.join(',');
          }
        }).catch(error => {
          console.log(error);
        });
      }
    },
    //执行传感器厂商
    create(formName) {
      const set = this.$refs;
      const self = this;
      set[formName].validate(valid => {
        if (valid) {
          // eslint-disable-next-line no-unused-vars
          addObj(this.form).then(res => {
            this.dialogFormVisible = false;

            this.$refs[formName].resetFields();

            this.getList();
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            });
          }).catch(error => {
            console.log(error);
            self.$refs[formName].resetFields();
          });
        } else {
          return false;
        }
      });
    },
    //执行采集器厂商
    deviceCreate(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          deviceAddObj(this.deviceform).then(() => {
            this.devicedialogFormVisible = false;
            this.$refs[formName].resetFields();
            this.deviceHandlerest();
            this.getDeviceList();
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            });
          }).catch(error => {
            console.log(error);
          });
        } else {
          return false;
        }
      });
    },
    //传感器取消按钮
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
      this.handlerest(formName);
    },
    //采集厂商  取消按钮
    deviceCancel(formName) {
      this.devicedialogFormVisible = false;
      this.$refs[formName].resetFields();
      this.deviceHandlerest(formName);
    },
    //执行传感器厂商
    update(formName) {
      const set = this.$refs;
      const self = this;
      set[formName].validate(valid => {
        if (valid) {
          this.form.id = this.sensorId;
          putObj(this.form).then(res => {
            this.dialogFormVisible = false;
            this.$refs[formName].resetFields();
            this.handlerest();
            this.getList();
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            });
          }).catch(error => {
            self.$refs[formName].resetFields();
            self.handlerest();
            console.log(error);
          });
        } else {
          return false;
        }
      });
    },
    //执行采集场修改
    deviceUpdate(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          devicePutObj(this.deviceform).then(() => {
            this.devicedialogFormVisible = false;

            this.$refs[formName].resetFields();
            this.deviceHandlerest();
            this.getDeviceList();
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            });
          }).catch(error => {
            console.log(error);
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.xf-query-input,
.xf-query-select {
  width: 200px;
  margin: 0 10px;
}
.fulldialog > div {
  width: 500px;
  margin: 0 auto;
}
.fulldialog .el-dialog__title {
  font-size: 32px;
}
.fulldialog {
  .el-dialog__header {
    margin-top: 20px;
  }
  .el-checkbox {
    display: block;
    margin-left: 0;
  }
  .custom-dialog-btn {
    position: absolute;
    font-size: 30px;
    left: 40px;
    top: 20px;
    color: #fff;
    &:hover {
      cursor: pointer;
    }
  }
  .dialog-footer {
    text-align: left;
  }
}
</style>
